#include "bthread.h"

bthread::bthread()
{
	m_threadid  = 0;
	m_created   = false;
	m_finished  = false;
}

bthread::~bthread()
{
	StopThread();
}

void bthread::StopThread()
{
	if ( (m_created) && (!m_finished) )
	{ 
	   m_finished = true;
	   WaitFor();
	};
}

void bthread::Execute()
{	
}

bool bthread::InitThread(bool a_suspend)
{
	sigset_t signal_mask;
	sigemptyset (&signal_mask);
	sigaddset (&signal_mask, SIGPIPE);
	int rc = pthread_sigmask (SIG_BLOCK, &signal_mask, NULL);
	if (rc != 0) 
       printf("block sigpipe error\n");

	m_created = pthread_create(&m_threadid, NULL, &Run, this) != 0;
	return m_created;
}

bool bthread::WaitFor()
{
    return pthread_join(m_threadid, NULL) != 0;
}

void* bthread::Run(void* a_thread)
{
     ( (bthread*)a_thread ) -> Execute();
}
